package com.boang;

import com.boang.dao.SpuDao;
import com.boang.po.Spu;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/*
  @author: 13813
  @date: 2024/7/23 16:40
*/
public class FirstDemo {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        SpuDao spuDao = sqlSession.getMapper(SpuDao.class);
        Spu spu = new Spu(104553, "华为为", null);
        //先从一级缓存查找，如果有，则返回，如果没有，则查询数据库，并把数据放到缓存中
        Spu sp1 = spuDao.selectSpuByIdAndName(spu);
        System.out.println(sp1);

        //增，删，改 清空缓存
        sqlSession.clearCache();//清空缓存

        Spu sp2 = spuDao.selectSpuByIdAndName(spu);
        System.out.println(sp2);
    }
}
